<?php

header('Content-Type: text/html; charset=Utf-8');

$devel_totals = array(
	'tests' => 0,
	'errors' => 0
);

$release_totals = array(
	'tests' => 0,
	'errors' => 0
);

if (empty($_GET)) {

    echo "<h1>Devel test</h1>";

    require '../devel/ITypoStrategy.php';
    require '../devel/DevelTypoStrategy.php';
    require '../devel/DevelTypo.php';

    foreach (glob('data/*.dat') as $file) {
        echo preform_tests(array('DevelTypo', 'processHtml'), $file, $devel_totals);
    }
    
    print_totals($devel_totals);

} else {

    echo "<h1>Realase test</h1>";

    require '../realases/php5/Typo.php';

    foreach (glob('data/*.dat') as $file) {
        echo preform_tests(array('Typo', 'processHtml'), $file, $release_totals);
    }
    
    print_totals($release_totals);
}


function print_totals($total){
	echo '<hr/>Total errors/tests: '.$total['errors'].'/'.$total['tests'];
}

function preform_tests($func, $test_file, &$totals) {

    $rez = '';

    $execute_test = true;
    $n_errors = 0;
    $n_tests  = 0;

    foreach (file($test_file) as $line_number => $str) {

        $str = trim($str);
        if (!$str) continue;

        $str = strtr($str, array(
            '\r' => "\r",
            '\n' => "\n",
        ));

        if ($execute_test) {

            $in  = $str;
            $out = call_user_func($func, $in);
            $n_tests++;
            $totals['tests']++;

        } else {

            $cfg = $str;

            if ($out != $cfg) {

                $cfg = strtr($cfg, array(
                    '&nbsp;' => '•',
                    '<' => '&lt;',
                    '>' => '&gt;',
                ));

                $out = strtr($out, array(
                    '&nbsp;' => '•',
                    '<' => '&lt;',
                    '>' => '&gt;',
                ));

                //var_dump($cfg);
                //echo "<br>";
                //var_dump($out);
                //echo "<br>";

                $rez .= "<div>Error at line: ".++$line_number."</div>\n";

                $rez .= "<pre>";
                $rez .= " IN: $in\n";
                $rez .= "OUT: $out\n";
                $rez .= "CFG: $cfg\n";
                $rez .= "</pre><hr>";
                $n_errors++;
                $totals['errors']++;
            }
        }

        $execute_test = !$execute_test;
    }

    if (!$execute_test) $rez .= "WARNING! [$test_file] is corrupted!\n";

    $rez = "<h1>$test_file: </h1>
        <div>errors: $n_errors  / $n_tests</div><br>
        $rez";

    return $rez;
}